import type {Course} from '../api/divide/course/types.gen';

export interface CourseFilters {
    keyword?: string;
    level?: string;
    category?: string;
    minPrice?: number;
    maxPrice?: number;
}

export const formatPrice = (price: number): string => {
    return `¥${price.toFixed(2)}`;
};

export const formatDuration = (minutes: number): string => {
    if (minutes < 60) {
        return `${minutes}分钟`;
    }
    const hours = Math.floor(minutes / 60);
    const remainingMinutes = minutes % 60;
    return remainingMinutes > 0 ? `${hours}小时${remainingMinutes}分钟` : `${hours}小时`;
};

export const getLevelColor = (level: string): string => {
    switch (level) {
        case '初级':
            return 'green';
        case '中级':
            return 'orange';
        case '高级':
            return 'red';
        default:
            return 'blue';
    }
};

export const getStatusText = (status: string): string => {
    switch (status) {
        case 'PUBLISHED':
            return '已发布';
        case 'DRAFT':
            return '草稿';
        case 'ARCHIVED':
            return '已归档';
        default:
            return status;
    }
};

export const filterCourses = (courses: Course[], filters: CourseFilters): Course[] => {
    return courses.filter(course => {
        if (filters.keyword && !course.title.toLowerCase().includes(filters.keyword.toLowerCase()) &&
            !course.description?.toLowerCase().includes(filters.keyword.toLowerCase())) {
            return false;
        }

        if (filters.level && course.level !== filters.level) {
            return false;
        }

        if (filters.minPrice && course.price < filters.minPrice) {
            return false;
        }

        if (filters.maxPrice && course.price > filters.maxPrice) {
            return false;
        }

        return true;
    });
};

export const sortCourses = (courses: Course[], sortBy: string): Course[] => {
    const sorted = [...courses];

    switch (sortBy) {
        case 'price-asc':
            return sorted.sort((a, b) => a.price - b.price);
        case 'price-desc':
            return sorted.sort((a, b) => b.price - a.price);
        case 'popularity':
            return sorted.sort((a, b) => (b.soldCount || 0) - (a.soldCount || 0));
        case 'newest':
            return sorted.sort((a, b) => new Date(b.createTime).getTime() - new Date(a.createTime).getTime());
        case 'rating':
            return sorted.sort((a, b) => (b.rating || 0) - (a.rating || 0));
        default:
            return sorted;
    }
};

export const getCourseStats = (courses: Course[]) => {
    if (!courses || courses.length === 0) {
        return {
            totalCourses: 0,
            avgPrice: 0,
            priceRange: {min: 0, max: 0},
            levelDistribution: {},
            categoryDistribution: {}
        };
    }

    const prices = courses.map(c => c.price);
    const levels = courses.map(c => c.level);
    const categories = courses.map(c => c.category);

    return {
        totalCourses: courses.length,
        avgPrice: prices.reduce((sum, price) => sum + price, 0) / prices.length,
        priceRange: {
            min: Math.min(...prices),
            max: Math.max(...prices)
        },
        levelDistribution: levels.reduce((acc, level) => {
            acc[level] = (acc[level] || 0) + 1;
            return acc;
        }, {} as Record<string, number>),
        categoryDistribution: categories.reduce((acc, category) => {
            acc[category] = (acc[category] || 0) + 1;
            return acc;
        }, {} as Record<string, number>)
    };
};